import 'package:flutter/material.dart';
import 'package:flutter_enjoy/apis/home.dart';
import 'package:flutter_enjoy/pagess/home/components/mode1.dart';
import 'package:flutter_enjoy/pagess/home/components/mode2.dart';
import 'package:flutter_enjoy/utils/RequestDio.dart';
import 'package:flutter_enjoy/utils/toast.dart';
import 'package:fluttertoast/fluttertoast.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  Map homeList = {};
  List announcementList = [];
  @override
  void initState() {
    super.initState();
    getHomeList();
  }

  getHomeList() async {
    final res = await HomeApi.getAnnouncementList();
    print(res);
    setState(() {
      announcementList = res;
    });
  }

  @override
  Widget build(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;
    return Scaffold(
      appBar: AppBar(
        title: const Text('享+本地生活'),
        centerTitle: true,
        backgroundColor: const Color(0xFFe5e8f1),
      ),
      body: Container(
        color: const Color(0xFFe5e8f1),
        child: ListView(
          children: [
            TextButton(
              onPressed: () {
                showDialog(
                    context: context,
                    builder: (context) {
                      return AlertDialog(
                        title: const Text('提示'),
                        content: const Text('这是一个提示'),
                        actions: [
                          TextButton(
                              onPressed: () {
                                Navigator.pop(context);
                              },
                              child: const Text('确定')),
                          TextButton(
                              onPressed: () {
                                Navigator.pop(context);
                              },
                              child: const Text('取消')),
                        ],
                      );
                    });
              },
              child: const Text('弹窗'),
            ),

            TextButton(
                onPressed: () async {
                  print(announcementList);
                },
                child: const Text('列表请求')),
            TextButton(
                onPressed: () async {
                  final res = await requestDio.get('/house');
                  print(res);
                },
                child: const Text('token请求')),
            TextButton(
                onPressed: () async {
                  final res = await HomeApi.getAnnouncementList();
                  print(res);
                },
                child: const Text('get请求')),
            TextButton(
                onPressed: () {
                  PromptAction.showSuccess('成功');
                },
                child: const Text('测试弹窗')),

            // mode1
            Container(
              child: const Mode1(),
            ),
            Container(
              child: Column(
                children: [
                  Stack(
                    children: [
                      // 背景色
                      Container(
                        height: screenWidth * 0.32,
                        color: const Color(0xFFe5e8f1),
                      ),
                      // banner图片
                      Positioned(
                        left: screenWidth * 0.04,
                        right: screenWidth * 0.04,
                        top: screenWidth * 0.04,
                        child: ClipRRect(
                          borderRadius:
                              BorderRadius.circular(screenWidth * 0.03),
                          child: Image.asset(
                            'assets/images/banner@2x.jpg',
                            fit: BoxFit.cover,
                            width: double.infinity,
                            height: screenWidth * 0.24,
                          ),
                        ),
                      ),
                    ],
                  ),
                  // 公告栏标题
                  Padding(
                    padding: EdgeInsets.only(
                        left: screenWidth * 0.06,
                        top: screenWidth * 0.04,
                        bottom: screenWidth * 0.02),
                    child: const Row(
                      children: [
                        Icon(Icons.campaign,
                            color: Color(0xFFf5b94b), size: 24),
                        SizedBox(width: 8),
                        Text.rich(
                          TextSpan(
                            children: [
                              TextSpan(
                                text: '社区',
                                style: TextStyle(
                                    fontSize: 18,
                                    fontWeight: FontWeight.bold,
                                    color: Colors.black87),
                              ),
                              TextSpan(
                                text: '公告',
                                style: TextStyle(
                                    fontSize: 18,
                                    fontWeight: FontWeight.bold,
                                    color: Color(0xFFf5b94b)),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                  // 公告卡片
                  Padding(
                    padding:
                        EdgeInsets.symmetric(horizontal: screenWidth * 0.06),
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(16),
                        boxShadow: const [
                          BoxShadow(
                            color: Colors.black12,
                            blurRadius: 8,
                            offset: Offset(0, 2),
                          ),
                        ],
                      ),
                      padding: const EdgeInsets.all(16),
                      margin: const EdgeInsets.only(bottom: 10),
                      child: Mode2(announcementList: announcementList),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
